package com.lovo.pz.util;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lovo.pz.role.BaseRole;
import com.lovo.pz.role.BucketheadZombieRole;
import com.lovo.pz.role.SunSeedRole;
import com.lovo.pz.role.ZombieRole;
import com.lovo.pz.window.GamePanel;

/**
 * Copyright (C), 2006-2011, ChengDu Lovo info. Co., Ltd.
 * FileName: GameUtil.java
 * 游戏的工具类
 *
 * @author  白德柯
 * @Date    2012-1-10
 * @version 1.00
 */
public class GameUtil {
	/** 普通僵尸走路的图片 */
	public static Image zombieWalkImage;
	/** 普通僵尸攻击的图片 */
	public static Image zombieAttackImage;
	/** 僵尸脑袋的图片 */
	public static Image zombieHeadImage;
	/** 无头僵尸走路的图片 */
	public static Image zombieLostHeadWalkImage;
	/** 无头僵尸攻击的图片 */
	public static Image zombieLostHeadAttackImage;
	/** 僵尸死掉的图片 */
	public static Image zombieDeadImage;
	/** 铁桶僵尸走路的图片 */
	public static Image bucketheadZombieWalkImage;
	/** 铁桶僵尸攻击的图片 */
	public static Image bucketheadZombieAttackImage;




	/** 欢迎界面的背景图片 */
	public static Image welcomeImage;
	/** 标题界面的背景图片 */
	public static Image titleImage;
	/** 排行榜界面的背景图片 */
	public static Image rankBackgroundImage;
	/** 标题界面上开始游戏按钮的图片 */
	public static Image [] startGameImage;
	/** 标题界面上开始游戏按钮的图片 */
	public static Image  showNameImage;
	/** 标题界面上开始游戏按钮的图片 */
	public static Image [] switchNameImage;
	/** 标题界面上开始游戏按钮的图片 */
	public static Image [] miniGameImage;
	/** 标题界面上开始游戏按钮的图片 */
	public static Image [] decodeGameImage;
	/** 标题界面上开始游戏按钮的图片 */
	public static Image [] liveImage;
	/** 标题界面上开始游戏按钮的图片 */
	public static Image [] rankImage;
	/** 标题界面上开始游戏按钮的图片 */
	public static Image [] helpImage;
	/** 标题界面上开始游戏按钮的图片 */
	public static Image [] exitImage;

	public static Image optionsMenubackImage;
	public static Image [] OptionsBackButtonImage;
	/** 游戏地图图片 */
	public static Image mapImage;
	/** 植物面板图片 */
	public static Image plantsPanelImage;
	/** 太阳种子图片 */
	public static Image sunSeedImage;
	/** 植物卡片图片 */
	public static Image [] plantsCardImage;

	/** 豌豆射手图片 */
	public static Image peaShooterImage;

	/** 太阳花图片 */
	public static Image sunFlowerImage;

	/** 豌豆射手子弹图片 */
	public static Image peaShooterBulletImage;
	/** 豌豆射手子弹爆炸图片 */
	public static Image peaShooterBulletBomImage;
	/** 汽车图片*/
	public static Image carImage;
	/**
	 * 铲子图片
	 */
	public static Image shovelImage;

	/**
	 * 关卡进度图片
	 */
	public static Image [] flagMeterImage;

	/**
	 * 关卡进度字体图片
	 */
	public static Image flagMeterLevelProgressImage;
	/**
	 * 关卡进度上的僵尸头
	 */
	public static Image flagMeterParts1Image;
	/**
	 * 关卡进度上的旗帜
	 */
	public static Image flagMeterParts2Image;

	/**
	 * 准备图片
	 */
	public static Image startReadyImage;
	/**
	 * 安放图片
	 */
	public static Image startSetImage;
	/**
	 * 植物图片
	 */
	public static Image startPlantImage;
	/**
	 * 僵尸胜利图片
	 */
	public static Image zombiesWonImage;
	/**
	 * 一大拨僵尸接近图片
	 */
	public static Image largeWaveImage;
	/**
	 * 最后一波僵尸图片
	 */
	public static Image finalWaveImage;

	public static String mainMusic = "sounds/mainmusic.mp3";
	public static String peaShooterBulletMusic = "sounds/butter.ogg";

	/**
	 * 用于初始化游戏中图片的方法
	 */
	public static void initImage(Component frame, MediaTracker tracker){
		flagMeterLevelProgressImage = GameImage.loadImage(tracker, "images/FlagMeterLevelProgress.png");
		flagMeterParts1Image = GameImage.loadImage(tracker, "images/FlagMeterParts1.png");
		flagMeterParts2Image = GameImage.loadImage(tracker, "images/FlagMeterParts2.png");
		flagMeterImage = GameImage.cutImage(tracker, frame, GameImage.VERTICAL, "images/FlagMeter.png", 2, 157, 21);

		welcomeImage = GameImage.loadImage(tracker, "images/welcome.png");
		titleImage = GameImage.loadImage(tracker, "images/title.png");
		rankBackgroundImage = GameImage.loadImage(tracker, "images/Rank_Background.jpg");

		showNameImage = GameImage.loadImage(tracker, "images/SelectorScreen_WoodSign1.png");

		startGameImage = GameImage.cutImage(tracker, frame, GameImage.VERTICAL, "images/startgame.png", 2, 331, 146);
		switchNameImage = GameImage.cutImage(tracker, frame, GameImage.VERTICAL, "images/switch_name.png", 2, 291, 71);
		miniGameImage = GameImage.cutImage(tracker, frame, GameImage.VERTICAL, "images/xiaoyouxi.png", 2, 313, 131);
		decodeGameImage = GameImage.cutImage(tracker, frame, GameImage.VERTICAL, "images/jiemi.png", 2, 313, 131);
		liveImage = GameImage.cutImage(tracker, frame, GameImage.VERTICAL, "images/shengcun.png", 2, 313, 131);

		rankImage = GameImage.cutImage(tracker, frame, GameImage.VERTICAL, "images/rank.png", 2, 81, 31);
		helpImage = GameImage.cutImage(tracker, frame, GameImage.VERTICAL, "images/help.png", 2, 48, 22);
		exitImage = GameImage.cutImage(tracker, frame, GameImage.VERTICAL, "images/exit.png", 2, 47, 27);

		optionsMenubackImage = GameImage.loadImage(tracker, "images/OptionsMenuback32.png");
		OptionsBackButtonImage = GameImage.cutImage(tracker, frame, GameImage.VERTICAL, "images/OptionsBackButton32.png", 2, 360, 100);

		mapImage = GameImage.loadImage(tracker, "images/background.jpg");
		plantsPanelImage = GameImage.loadImage(tracker,"images/SeedBank.png");

		sunSeedImage = GameImage.loadImage(tracker,"images/Sun.gif");

		plantsCardImage = GameImage.cutImage(tracker, frame, GameImage.HORIZONTAL, "images/plants_title.png", 6, 50, 70);

		peaShooterImage = GameImage.loadImage(tracker,"images/Peashooter.gif");
		sunFlowerImage = GameImage.loadImage(tracker,"images/SunFlower.gif");

		peaShooterBulletImage = GameImage.loadImage(tracker,"images/pea_bullet.png");
		peaShooterBulletBomImage = GameImage.loadImage(tracker,"images/PeaBulletHit.gif");

		shovelImage = GameImage.loadImage(tracker,"images/Shovel.png");
		carImage = GameImage.loadImage(tracker,"images/car.png");

		zombieWalkImage = GameImage.loadImage(tracker, "images/image_zombies/Zombie/Zombie.gif");
		zombieAttackImage = GameImage.loadImage(tracker, "images/image_zombies/Zombie/ZombieAttack.gif");
		zombieHeadImage = GameImage.loadImage(tracker, "images/image_zombies/Zombie/ZombieHead.gif");
		zombieLostHeadWalkImage = GameImage.loadImage(tracker, "images/image_zombies/Zombie/ZombieLostHead.gif");
		zombieLostHeadAttackImage = GameImage.loadImage(tracker, "images/image_zombies/Zombie/ZombieLostHeadAttack.gif");
		zombieDeadImage = GameImage.loadImage(tracker, "images/image_zombies/Zombie/ZombieDie.gif");

		bucketheadZombieWalkImage = GameImage.loadImage(tracker, "images/image_zombies/BucketheadZombie/BucketheadZombie.gif");
		bucketheadZombieAttackImage = GameImage.loadImage(tracker, "images/image_zombies/BucketheadZombie/BucketheadZombieAttack.gif");

		startReadyImage = GameImage.loadImage(tracker,"images/StartReady.png");
		startSetImage = GameImage.loadImage(tracker,"images/StartSet.png");
		startPlantImage = GameImage.loadImage(tracker,"images/StartPlant.png");
		zombiesWonImage = GameImage.loadImage(tracker,"images/ZombiesWon.png");
		largeWaveImage = GameImage.loadImage(tracker,"images/LargeWave.gif");
		finalWaveImage = GameImage.loadImage(tracker,"images/FinalWave.png");
	}

	/**
	 * 产生种子的方法
	 */
	public static void generateSunSeed(final GamePanel gamePanel){
		//使用匿名内部类启动线程
		Thread th = new Thread(){
			public void run(){
				while(gamePanel.getGameStatus() == BaseRole.GAME_START){
					SunSeedRole sunSeedRole = new SunSeedRole(gamePanel);
					sunSeedRole.setRoleX((int)(Math.random()*700) + 100);
					gamePanel.getSunSeedRoleList().add(sunSeedRole);
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		th.start();
	}
	/**
	 * 指定产生僵尸的数量
	 */
	public static void generateZombie(final GamePanel gamePanel, int count){
		Thread th = new Thread(){
			public void run(){
				for(int i = 0;i < count;i++){
					gamePanel.getZombieRoleList().add(zombieRoleFactory(gamePanel));
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		th.start();
	}
	/**
	 * 产生僵尸
	 */
	public static void generateZombie(final GamePanel gamePanel){
		Thread th = new Thread(){
			public void run(){
				while(gamePanel.getGameStatus() == BaseRole.GAME_START){
					gamePanel.getZombieRoleList().add(zombieRoleFactory(gamePanel));
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		th.start();
	}

	/**
	 * 随机产生不同僵尸的方法
	 * @param gamePanel
	 * @return
	 */
	private static ZombieRole zombieRoleFactory(GamePanel gamePanel){
		Class [] zombieClass = {
				ZombieRole.class,
				BucketheadZombieRole.class
		};
		int index = (int)(Math.random() * zombieClass.length);
		try{
			return (ZombieRole) zombieClass[index].getDeclaredConstructor(Component.class)
					.newInstance(gamePanel);
		}catch(Exception e){}
		return new ZombieRole(gamePanel);
	}
	/**
	 * 指定延迟时间
	 * @param time 延迟时间
	 * @param obj 指定在哪个对象的方法中进行延迟
	 * @param symbol 用于区分同一个对象中的延迟方法调用
	 * @return
	 */
	private static Map<String,Long> timeMap = new HashMap();



	public static boolean delay(long time,Object obj,String symbol){
		if(!timeMap.containsKey(obj.hashCode() + symbol)){
			timeMap.put(obj.hashCode() + symbol, time/15);
		}else{
			long delayTime = timeMap.get(obj.hashCode() + symbol);
			timeMap.put(obj.hashCode() + symbol, --delayTime);
		}
		if(timeMap.get(obj.hashCode() + symbol) <= 0){
			timeMap.remove(obj.hashCode() + symbol);
			return true;
		}
		return false;
	}
	/**
	 * 判断鼠标是否进入某个角色范围的方法
	 * @param x 鼠标的X坐标
	 * @param y 鼠标的Y坐标
	 * @param role 需要判断的角色对象
	 */
	public static boolean checkRoleRect(int x,int y,BaseRole role){
		if(x > role.getRoleX() && x < role.getRoleX() + role.getRoleWidth()
				&& y > role.getRoleY() && y < role.getRoleY() + role.getRoleHeight()){
			return true;
		}
		return false;
	}
	/**
	 * 计算两点之间所有坐标点
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @return
	 */
	public static List getLinePoint(int x1,int y1, int x2,int y2){
		List<Point> list = new ArrayList<Point>();
		if (x1 == x2) {
			// Tangent = NaN
			int from = Math.min(y1, y2);
			int to = Math.max(y1, y2);
			for (int y = from; y <= to; y++) {
				list.add(new Point(x1, y));
			}
		} else {
			double slope = ((double) (y2 - y1)) / ((double) (x2 - x1));
			int step = (x2 > x1) ? 1 : -1;
			for (int x = x1; x != x2; x += step) {
				int y = (int)((x - x1) * slope + y1);
				list.add(new Point(x, y));
			}
		}
		return list;
	}

	/**
	 * 计算坐标
	 * @return
	 */
	public static Point computerPosition(int x,int y){
		Point point = null;
		if(x > 150 && x < 852 && y > 110 && y < 570){
			point = new Point();
			int posX = x - 150;
			int posY = y - 110;
			int row = (int)Math.floor(posY / 92);
			int col = (int)Math.floor(posX / 78);
			point.setLocation(col * 78 + 150, row * 92 + 110);

		}
		return point;
	}

	/**
	 * 检测两个角色是否碰撞
	 * @param role1
	 * @param role2
	 * @return 发生碰撞则返回TRUE，否则返回false
	 */
	public static boolean checkImpact(BaseRole role1,BaseRole role2){
		if(role1 == null || role2 == null){
			return false;
		}
		if(role1.getRoleX() + role1.getRoleWidth() >= role2.getRoleX() &&
				role1.getRoleY() + role1.getRoleHeight() >= role2.getRoleY() &&
				role2.getRoleX() + role2.getRoleWidth() >= role1.getRoleX() &&
				role2.getRoleY() + role2.getRoleHeight() >= role1.getRoleY()){
			return true;
		}
		return false;
	}

	/**
	 * 设置透明
	 * @param g 画笔
	 * @param alpha 透明值 0-1之间
	 */
	public static void setAlpha(Graphics g,float alpha){
		//源排斥目标法合成规则
		int compositeRule = AlphaComposite.SRC_OVER;
		//创建AlphaComposite对象
		AlphaComposite alphaComposite=AlphaComposite.getInstance(compositeRule,alpha);
		//设置图形环境的合成方式
		((Graphics2D)g).setComposite(alphaComposite);
	}
	/**
	 * 封装休眠的方法
	 * @param time
	 */
	public static void sleep(long time){
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

